{% extends "tools/base_tool.html" %}

{% block title %}{{ get_text(tool.name) if tool else 'Stirling PDF 工具集' }}{% endblock %}

{% block content %}
<div class="container mt-5">
    <div class="d-flex justify-content-center align-items-center" style="min-height: 70vh;">
        <div class="card shadow-sm">
            <div class="card-body text-center p-4">
                <h4 class="card-title mb-3">
                    {% if tool %}{{ get_text(tool.name) }}{% else %}Stirling PDF 工具集{% endif %}
                </h4>
                <p class="card-text mb-3">
                    {{ get_text('stirling_pdf_redirect_message_title', '正在为您跳转到 Stirling PDF 工具集...') }}
                </p>
                <div class="spinner-border text-primary mb-3" role="status" style="width: 3rem; height: 3rem;">
                    <span class="visually-hidden">Loading...</span>
                </div>
                <p class="card-text text-muted mb-4">
                    {{ get_text('stirling_pdf_redirect_message_manual_link', '如果浏览器没有自动跳转，请点击下面的按钮。') }}
                </p>
                <div id="redirect-message-area" class="alert mt-3" role="alert" style="display: none;"></div>
                <a href="{{ tool_external_url | e }}" target="_blank" class="btn btn-primary btn-lg">
                    {# Assuming Bootstrap Icons are used, you might need to ensure they are available #}
                    {# <i class="bi bi-box-arrow-up-right me-2"></i> #}
                    {{ get_text('stirling_pdf_redirect_manual_button', '手动打开 Stirling PDF') }}
                </a>
                 <p class="mt-3"><a href="/">{{ get_text('back_to_home', '返回首页') }}</a></p>
            </div>
        </div>
    </div>
</div>

<script>
    (function() {
        var externalUrl = "{{ tool_external_url | e }}"; // Using Jinja's escape filter for security
        var messageArea = document.getElementById('redirect-message-area');

        function showMessage(text, type) {
            if (messageArea) {
                messageArea.textContent = text;
                messageArea.className = 'alert alert-' + type + ' mt-3'; // Add Bootstrap classes and margin
                messageArea.style.display = 'block';
            }
        }

        // The 'tool_external_url' variable is expected to be passed from the Flask template context.
        // This context variable should hold the actual URL for the Stirling PDF tool, fetched from API_PDF in config.

        if (externalUrl && externalUrl !== "None" && externalUrl.trim() !== "") {
            try {
                var newWindow = window.open(externalUrl, '_blank');
                // Check if the new window was blocked by a popup blocker
                if (!newWindow || newWindow.closed || typeof newWindow.closed === 'undefined') {
                    console.warn("Popup blocked. Please allow popups for this site or click the manual link.");
                    showMessage("{{ get_text('stirling_pdf_popup_blocked_warning', '浏览器阻止了弹出窗口。请允许此站点的弹出窗口，或点击手动打开按钮。') }}", 'warning');
                } else {
                    // Optional: If you want to redirect the current page after a delay
                    // setTimeout(function() { window.location.href = "/"; }, 3000);
                }
            } catch (e) {
                console.error("Error attempting to open new window:", e);
                showMessage("{{ get_text('stirling_pdf_open_error', '尝试打开新窗口时发生错误。请稍后重试或手动打开。') }}", 'danger');
            }
        } else {
            console.error("Stirling PDF external URL is not configured or is invalid: ", externalUrl);
            showMessage("{{ get_text('stirling_pdf_invalid_url_error', 'Stirling PDF 的外部链接未配置或无效。请联系管理员。') }}", 'danger');
        }
    })();
</script>
{% endblock %}

{% block custom_js %}
{# Any additional tool-specific JS can go here if needed #}
{% endblock %}